﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace FlexiCommerce.Web
{
    public class ImageUtility
    {

        public static void ResizeAndSave(Image image, string fileName, string finalPath, ImageFormat format, int maxWidth, int maxHeight)
        {
            using (image)
            {
                double widthRatio = (double)image.Width / (double)maxWidth;
                double heightRatio = (double)image.Height / (double)maxHeight;
                double ratio = Math.Max(widthRatio, heightRatio);
                int newWidth = (int)(image.Width / ratio);
                int newHeight = (int)(image.Height / ratio);


                using (System.Drawing.Bitmap newBMP = new Bitmap(newWidth, newHeight,
                      PixelFormat.Format24bppRgb))
                {
                    using (System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(newBMP))
                    {
                        oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        oGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        oGraphics.CompositingQuality = CompositingQuality.HighQuality;
                        oGraphics.DrawImage(image, 0, 0, newWidth, newHeight);
                        newBMP.Save(string.Format("{0}{1}", finalPath, fileName), image.RawFormat);
                    }
                }
            }
        }
        //public static void ResizeAndSave(Image image, string fileName, string finalPath,ImageFormat format, int maxWidth, int maxHeight)
        //{
        //    using (image) {
        //        // Calculate the new image dimensions
        //        int width = image.Width;
        //        //actual width
        //        int height = image.Height;
        //        //actual height
        //        int widthDiff = (width - maxWidth);
        //        //how far off maxWidth?
        //        int heightDiff = (height - maxHeight);
        //        //how far off maxHeight?
        //        //figure out which dimension is further outside the max size
        //        bool doWidthResize = (maxWidth > 0 && width > maxWidth && widthDiff > -1 && (widthDiff > heightDiff || maxHeight.Equals(0)));
        //        bool doHeightResize = (maxHeight > 0 && height > maxHeight && heightDiff > -1 && (heightDiff > widthDiff || maxWidth.Equals(0)));

        //        //only resize if the image is bigger than the max or where image is square, and the diffs are the same
        //        if (doWidthResize || doHeightResize || (width.Equals(height) && widthDiff.Equals(heightDiff))) {
        //            int iStart = 0;
        //            decimal divider = default(decimal);
        //            if (doWidthResize) {
        //                iStart = width;
        //                divider = Math.Abs((decimal)iStart / (decimal)maxWidth);
        //                width = maxWidth;
        //                height = (int)Math.Round((decimal)(height / divider));
        //            }
        //            else {
        //                iStart = height;
        //                divider = Math.Abs((decimal)iStart / (decimal)maxHeight);
        //                height = maxHeight;
        //                width = (int)Math.Round((decimal)(width / divider));
        //            }
        //        }
        //        PixelFormat Format = image.PixelFormat;
        //        if (Format.ToString().Contains("Indexed"))
        //            Format = PixelFormat.Format24bppRgb;

        //        using (System.Drawing.Bitmap newBMP = new System.Drawing.Bitmap(image, width, height))
        //        {
        //            using (System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(newBMP)) {
        //                oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //                oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        //                oGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        //                oGraphics.CompositingQuality = CompositingQuality.HighQuality;

        //                oGraphics.DrawImage(image,new Rectangle(0,0, width, height));
        //                newBMP.Save(string.Format("{0}{1}", finalPath, fileName),image.RawFormat);
        //            }
        //        }
        //    }
        //}


    }
}
